﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _13.MenuTasks
{
    class MenuTasks
    {
        static void Main()
        {
            string menu =
             @"         MENU

         1) Reverses the digits of a number
­         2) Calculates the average of a sequence of integers
­         3) Solves a linear equation a * x + b = 0
            1, 2 or 3?
            Choice: ";
            Console.Write(menu);
            string choiceString = "";
            Choices(menu, choiceString);
        }

        private static void Choices(string menu, string choiceString)
        {
            int choice = CheckForEmpty(choiceString);


            if (choice < 1 | choice > 3)
            {

                Console.Clear();
                Console.WriteLine(menu);
                Console.WriteLine("Wrong input! Try again: Input 1,2 or 3..");
                Choices(menu, choiceString);
            }
            else if (choice == 1) ReverseDigits();
            else if (choice == 2) AverageOfSequence();
            else if (choice == 3) LinearEquation();
        }



        static int CheckForEmpty(string choiceString)
        {
            string choice = Console.ReadLine();

            if (choice == string.Empty)
            {
                while (true)
                {
                    Console.WriteLine("String is empty. Try again:");
                    choice = Console.ReadLine();
                    if (choice != string.Empty) break;
                }
            }
            int value = int.Parse(choice);
            return value;
        }



        static void ReverseDigits()
        {
            Console.WriteLine("Input the number to reverse:");
            int notReversedNumber = int.Parse(Console.ReadLine());
            Console.WriteLine("Not reversed: {0}", notReversedNumber);
            int reversedNumber = 0;
            while (notReversedNumber > 0)
            {
                reversedNumber = reversedNumber * 10 + (notReversedNumber % 10);
                notReversedNumber /= 10;
            }
            Console.WriteLine("Reversed = {0}", reversedNumber);
            Again();
        }



        static void AverageOfSequence()
        {
            Console.WriteLine("Calculates the average of a sequence of integers");
            Console.WriteLine("Sequence long (n):");
            string choiceString = "";
            int choice = CheckForEmpty(choiceString);

            int[] sequence = new int[choice];
            double sum = 0;
            for (int i = 0; i < sequence.Length; i++)
            {
                Console.WriteLine("sequence[{0}]=", i);
                string input = "";
                int value = CheckForEmpty(input);
                sequence[i] = value;
                sum += sequence[i];
            }
            Console.WriteLine("Average: {0}", sum / sequence.Length);
            Again();
        }

        static void LinearEquation()
        {
            Console.WriteLine("Solves a linear equation a * x + b = 0");
            Console.WriteLine(" a: ");
            int a = int.Parse(Console.ReadLine());
            if (a == 0)
            {
                while (true)
                {
                    Console.WriteLine("must be a > 0. Try again:");
                    a = int.Parse(Console.ReadLine());
                    if (a != 0)
                    {
                        break;
                    }
                }
            }
            Console.WriteLine(" b: ");
            int b = int.Parse(Console.ReadLine());

            Console.WriteLine(" x: ");
            int x = int.Parse(Console.ReadLine());

            Console.WriteLine("Result of {0} * {1} + {2} = ? is : {3}", a, x, b, (a * x) + b);
            Again();

        }

        static void Again()
        {
            Console.WriteLine(@"Again:? (yes/no)");
            string choice = Console.ReadLine();
            
            if ((choice == "yes" | choice == "no") && !(choice == string.Empty))
            {
                if (choice == "yes")
                {
                    Console.Clear();
                    Main();
                }
                if (choice == "no")
                {
                    
                    Console.WriteLine("Bye!");
                }
            }
        }
    }
}